package com.wugh.fast.module.mapper.interceptor.plugin.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.wugh.fast.module.api.annotation.IsDeleted;
import com.wugh.fast.module.mapper.EntityInfo;
import com.wugh.fast.module.mapper.interceptor.plugin.AbstractAnnotationPlugin;
import com.wugh.fast.module.mapper.interceptor.plugin.PluginSwitch;
import lombok.Getter;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * 描述:
 *
 * @author 吴冠辉
 * @date 2018/8/7 11:49
 */
public class IsDeletedPlugin extends AbstractAnnotationPlugin<IsDeleted> {
    private String isDeleteFieldName;
    @Getter
    private Object deletedRecord;
    private Object isDeletedValue;
    private Object notDeletedValue;

    @Override
    public boolean support(EntityInfo entityInfo) {
        boolean support = super.support(entityInfo);
        if (!support) {
            return false;
        }
        this.isDeleteFieldName = this.getColumn().getEntityField().getName();

        this.isDeletedValue = TypeUtils.cast(this.getAnnotation().isDeleteValue(), this.getColumn().getJavaType(), null);
        this.notDeletedValue = TypeUtils.cast(this.getAnnotation().notDeleteValue(), this.getColumn().getJavaType(), null);
        this.deletedRecord = entityInfo.getConstructorAccess().newInstance();
        BiConsumer setter = entityInfo.getPropertySetter(isDeleteFieldName);
        setter.accept(deletedRecord, this.isDeletedValue);
        return true;
    }

    @Override
    public void autoHandleExample(Example example) {
        //在过滤条件中增加删除字段的过滤
        example.and().andEqualTo(isDeleteFieldName, this.notDeletedValue);
    }

    @Override
    public void autoHandleInsertList(List recordList) {
        if (this.getAnnotation().insertAuto()) {
            BiConsumer setter = entityInfo.getPropertySetter(isDeleteFieldName);
            recordList.forEach(record -> setter.accept(record, this.notDeletedValue));
        }
    }

    @Override
    public void autoHandleInsert(Object record) {
        if (this.getAnnotation().insertAuto()) {
            BiConsumer setter = entityInfo.getPropertySetter(isDeleteFieldName);
            setter.accept(record, this.notDeletedValue);
        }
    }

    @Override
    public void autoHandleInsertSelective(Object record) {
        if (this.getAnnotation().insertAuto()) {
            BiConsumer setter = entityInfo.getPropertySetter(isDeleteFieldName);
            setter.accept(record, this.notDeletedValue);
        }
    }

    /**
     * 关闭插件
     */
    public static void close() {
        PluginSwitch.close(IsDeletedPlugin.class);
    }

    /**
     * 指定实体类关闭插件
     *
     * @param entityClass
     */
    public static void close(Class<?> entityClass) {
        PluginSwitch.close(IsDeletedPlugin.class, entityClass);
    }

    /**
     * 开启插件
     */
    public static void open() {
        PluginSwitch.open(IsDeletedPlugin.class);
    }

    /**
     * 指定实体类开启插件
     * <pre>
     *     注意调用{@link IsDeletedPlugin#close()}后,调用此方法无效
     * </pre>
     *
     * @param entityClass
     */
    public static void open(Class<?> entityClass) {
        PluginSwitch.open(IsDeletedPlugin.class, entityClass);
    }

}
